package com.ruoyi.controller.system;

import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.vo.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.controller.BaseController;
import com.ruoyi.domain.SysDept;
import com.ruoyi.service.SysDeptService;

@RestController
@RequestMapping("system/dept")
public class SysDeptController extends BaseController{
	
	@Autowired
	private SysDeptService sysDeptService;
	
	/**
	 * 获取部门列表
	 * @param dept
	 * @return
	 */
	@GetMapping("list")
	public AjaxResult list(SysDept dept) {
		logger.info("获取部门列表,dept:{}",dept);
		List<SysDept> list = sysDeptService.selectDeptList(dept);
		return AjaxResult.success(list);
	}
	
	/**
	 * 根据部门编号获取信息
	 * @param deptId
	 * @return
	 */
	@GetMapping(value = "/{deptId}")
	public AjaxResult getInfo(@PathVariable Long deptId) {
		logger.info("根据部门编号获取信息,deptId:{}",deptId);
		return AjaxResult.success(sysDeptService.selectDeptById(deptId));
	}
	
	/**
	 * 查询部门列表
	 * @param deptId
	 * @return
	 */
	@GetMapping("list/exclude/{deptId}")
	public AjaxResult excludeChild(@PathVariable(value = "deptId",required = false) Long deptId) {
		logger.info("查询部门列表,deptId:{}",deptId);
		List<SysDept> depts = sysDeptService.selectDeptList(new SysDept());
		depts.removeIf(d -> d.getParentId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
		return AjaxResult.success(depts);
	}
	
	
	/**
	 * 新增部门
	 * @param dept
	 * @return
	 */
	@PostMapping
	public AjaxResult add(@Validated @RequestBody SysDept dept) {
		logger.info("新增部门,dept:{}",dept);
		if (!sysDeptService.checkDeptNameUnique(dept)) {
			return error ("新增部门" + dept.getDeptName() + "失败，部门名称已存在");
		}
		dept.setCreateBy(getUsername());
		return toAjax(sysDeptService.insertDept(dept));
	}
	
	/**
	 * 修改部门
	 * @param dept
	 * @return
	 */
	@PutMapping
	public  AjaxResult edit(@Validated @RequestBody SysDept dept) {
		logger.info("修改部门,dept:{}",dept);
		Long deptId = dept.getDeptId();
		if (!sysDeptService.checkDeptNameUnique(dept)) {
			return error ("修改部门" + dept.getDeptName() + "失败，部门名称已存在");
		} else if (dept.getParentId().equals(deptId)) {
			return error ("修改部门" + dept.getDeptName() + "失败，上级部门不能是自己");
		} else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && sysDeptService.selectNormalChildrenDeptById(deptId) > 0) {
			return error("该部门包含未停用的子部门！");
		}
		dept.setUpdateBy(getUsername());
		return toAjax(sysDeptService.updateDept(dept));
	}
	
	/**
	 * 删除部门
	 * @param deptId
	 * @return
	 */
	@DeleteMapping("/{deptId}")
	public AjaxResult remove(@PathVariable Long deptId) {
		logger.info("删除部门,deptId:{}",deptId);
		if (sysDeptService.hasChildByDeptId(deptId)) {
			return warn("存在下级部门，不允许删除");
		}
		if (sysDeptService.checkDeptExistUser(deptId)) {
			return warn("部门存在用户,不允许删除");
		}
		return toAjax(sysDeptService.deleteDeptById(deptId));
	}
	
}
